package game.module.hall.logic;

import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.common.ErrorCodeConstants;
import game.entity.PlayingRole;
import game.util.CommUtil;
import lua.LuaMessageLogin.S2ErrorCode;

/**
 * @author:Pomelo.lee
 * @date:2017年9月18日 下午7:36:47
 */
public class CheatManager {

	private static Logger logger = LoggerFactory.getLogger(CheatManager.class);

	static class SingletonHolder {
		static CheatManager instance = new CheatManager();
	}

	public static CheatManager getInstance() {
		return SingletonHolder.instance;
	}

	/**
	 * 打开通用防作弊
	 * 
	 * @param cheat
	 * @param ips
	 * @param hero
	 */
	public void openCheatComm(byte cheat, Set<String> ips, PlayingRole hero) {
		if (cheat == 0 || ips == null || hero == null) {
			return;
		}
		String newip = CommUtil.getIpAddr(hero.getGamePlayer().getAddress());
		ips.add(newip);
	}

	/**
	 * 核查是否作弊中，返回为true 为作弊
	 * 
	 * @param cheat
	 * @param ips
	 * @param hero
	 * @return
	 */
	public boolean checkCheatAndPush(byte cheat, Set<String> ips, PlayingRole hero) {
		if (cheat == 0 || ips == null || hero == null) {
			return false;
		}
		String newip = CommUtil.getIpAddr(hero.getGamePlayer().getAddress());
		if (ips.contains(newip)) {
			S2ErrorCode respMsg = new S2ErrorCode(ErrorCodeConstants.SAME_IP_NOT_ALLOW_ENTER_ROOM);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return true;
		}
		return false;
	}

	/**
	 * 核查是否作弊中，返回为true 为作弊 非作弊将加入中期并加入对应IP 在核查条件的最后调用
	 * 
	 * @param cheat
	 * @param ips
	 * @param hero
	 * @return
	 */
	public boolean checkCheatAndJoin(byte cheat, Set<String> ips, PlayingRole hero) {
		if (cheat == 0 || ips == null || hero == null) {
			return false;
		}
		if (checkCheatAndPush(cheat, ips, hero)) {
			return true;
		}
		String newip = CommUtil.getIpAddr(hero.getGamePlayer().getAddress());
		ips.add(newip);
		return false;
	}

}
